package org.eCon.maven.mojo;


import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.eCon.maven.iPlugin.IHost;
import org.eCon.maven.generatorFactory.GWTRunPluginGeneratorImpl;

import java.util.*;

/**
 * User: erkink
 * Date: 14-Aug-2008
 * Time: 15:57:18
 *
 * @goal run
 * @requiresDependencyResolution compile
 */
public class GWTRunMojo extends AbstractGWTMojo {
    /**
     * Project classpath.
     *
     * @parameter expression="${project.compileClasspathElements}"
     * @required
     * @readonly
     */
    private List classpathElements;

    /**
     * Project artifacts.
     *
     * @parameter expression="${project.artifact}"
     * @required
     * @readonly
     */
    private Artifact projectArtifact;

    /**
     * <p>
     * Sets the arguments to be passed to the compiler (prepending a dash) if fork is set to true.
     * </p>
     * <p>
     * This is because the list of valid arguments passed to a Java compiler
     * varies based on the compiler version.
     * </p>
     *
     * @parameter
     * @since 2.0.1
     */
    private Map javaArguments;

    /**
     * <p>
     * Sets the unformatted argument string to be passed to the compiler if fork is set to true.
     * </p>
     * <p>
     * This is because the list of valid arguments passed to a Java compiler
     * varies based on the compiler version.
     * </p>
     *
     * @parameter
     */
    private String javaArgument;

    // ----------------------------------------------------------------------
    // Read-only parameters
    // ----------------------------------------------------------------------

    /**
     * The current build session instance. This is used for
     * toolchain manager API calls.
     *
     * @parameter expression="${session}"
     * @required
     * @readonly
     */
    private MavenSession session;


    protected List getClasspathElements() {
        return classpathElements;
    }

    //protected abstract File getOutputDirectory();
    public void execute() throws MojoExecutionException {

        getLog().info("Start processing eCon's gwtCompiler plugin");
        this.checkPathes();

        IHost builder = new GWTRunPluginGeneratorImpl().createPlugin(this.gwt_os);
        getLog().info("Operating Sys: " + builder);
        getLog().info("GWT_HOME: " + this.gwt_Home);
        getLog().info("JAVA_HOME: " + this.java_Home);

        builder.setCurrentDir(this.currentDirAsFile.getAbsolutePath());
        builder.setLog(getLog());
        builder.setJava_Home(this.java_Home);
        builder.setGwt_Home(this.gwt_Home);
        builder.setGwt_module(this.gwt_module);
        builder.setGwt_relative_out(this.gwt_relative_out);
        builder.setGwt_relative_src(this.gwt_relative_src);
        builder.setAdditional_classpathes(additional_classpathes);
         if (getClasspathElements() != null) {
            for (int i = 0; i < getClasspathElements().size(); i++) {
                getLog().debug("ClassPath :" + getClasspathElements().get(i).toString());
            }
        }

        builder.setClasspathElements(getClasspathElements());
        builder.setProjectArtifact(projectArtifact);
        builder.setJavaArgument(javaArgument);
        builder.setJavaArguments(javaArguments);
        builder.setSession(session);
        builder.setJvm_param(this.jvm_param);



        builder.createHostModeScript();

    }
}
